package dao.impl;

import core.BaseDao;
import dao.BaseInfoDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Department;
import pojo.Worker;
import util.MyMapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseInfoDaoImpl extends BaseDao implements BaseInfoDao {
    /**
     * 修改员工信息
     * @param worker
     * @return
     */
    @Override
    public Integer edit(Worker worker) {
        try {
            String sql = "UPDATE worker SET worker_name=?,worker_gender=?,worker_phone=?,worker_salary=?,worker_id_card=?,worker_department_code=?,worker_update_time=? WHERE worker_id=?";
            return queryRunner.update(sql,
                    worker.getWorkerName(),
                    worker.getWorkerGender(),
                    worker.getWorkerPhone(),
                    worker.getWorkerSalary(),
                    worker.getWorkerIdCard(),
                    worker.getWorkerDepartmentCode(),
                    worker.getWorkerUpdateTime(),
                    worker.getWorkerId()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 真正的删除员工
     * @param workerId
     * @return
     */
    @Override
    public Integer realDelete(Integer workerId) {
        try {
            String sql = "DELETE FROM worker WHERE worker_id=?";
            return queryRunner.update(sql, workerId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 判断员工的编辑信息是否重复
     * @param worker
     * @return
     */
    @Override
    public Integer checkEditInfo(Worker worker) {
        try {
            sql = "SELECT COUNT(1) FROM worker WHERE worker_phone = ? AND worker_id != ?";
            if (Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), worker.getWorkerPhone(), worker.getWorkerId()).toString())>0) {
                return 1;
            }
            sql = "SELECT COUNT(1) FROM worker WHERE worker_id_card = ? AND worker_id != ?";
            if (Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), worker.getWorkerIdCard(), worker.getWorkerId()).toString())>0) {
                return 2;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 恢复员工
     * @param workerId
     * @return
     */
    @Override
    public Integer restore(Integer workerId) {
        try {
            String sql = "UPDATE worker SET worker_is_deleted=0 AND worker_update_time=? WHERE worker_id=?";
            return queryRunner.update(sql, new Timestamp(System.currentTimeMillis()), workerId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据员工id查询员工
     * @param workerId
     * @return
     */
    @Override
    public Worker findWorkerById(Integer workerId) {
        try {
            sql = "SELECT * FROM worker,department WHERE worker_id=? AND worker.worker_department_code=department.department_code";
            List<Map<String, Object>> queryList = queryRunner.query(sql, new MyMapListHandler(), workerId);
            Worker worker = new Worker();
            Department department = new Department();
            BeanUtils.populate(worker, queryList.get(0));
            BeanUtils.populate(department, queryList.get(0));
            worker.setDepartment(department);
            return worker;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 软删除员工
     * @param workerId
     * @return
     */
    @Override
    public Integer delete(Integer workerId) {
        try {
            String sql = "UPDATE worker SET worker_is_deleted=1,worker_update_time=? WHERE worker_id=?";
            return queryRunner.update(sql, new Timestamp(System.currentTimeMillis()), workerId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    /**
     * 展示所有未删除的员工（超级管理员除外）
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Worker> workerList(Integer page, Integer limit) {
        List<Worker> workerList = new ArrayList<>();
        try {
            sql = "SELECT * FROM worker,department WHERE worker_department_code !=4 AND worker_is_deleted = 0 AND worker.worker_department_code=department.department_code ORDER BY worker_id desc LIMIT ?,?";
            List<Map<String, Object>> query = queryRunner.query(sql, new MyMapListHandler(), (page - 1) * limit, limit);
            return WorkerDaoImpl.getWorkers(workerList, query);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 统计所有未删除员工的个数
     * @return
     */
    @Override
    public Integer count() {
        try {
            sql = "SELECT count(1) FROM worker WHERE worker_department_code !=4 AND worker_is_deleted=0";
            Object query = queryRunner.query(sql, new ScalarHandler<>());
            return Integer.valueOf(query.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
